// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

// 封装移动坐标
// 单例模式
export default class moveInput{
    private static instance: moveInput = null;

    // 水平轴
    horizontal: number = 0;
    // 垂直轴
    vertical: number = 0;

    static Instance() {
        if (!this.instance) {
            this.instance = new moveInput();
        }
        return this.instance;
    };
    // 通过private修饰符，让类无法在外部创建新的实例
    private constructor() { 
        // 键盘按下
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, (event)=>{
            switch(event.keyCode) {
                case cc.macro.KEY.w: this.vertical = 1; break;
                case cc.macro.KEY.s: this.vertical = -1; break;
                case cc.macro.KEY.a: this.horizontal = -1; break;
                case cc.macro.KEY.d: this.horizontal = 1; break;
            }
        })
        // 键盘抬起
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP, (event)=>{
            switch(event.keyCode) {
                case cc.macro.KEY.w: if(this.vertical == 1) this.vertical = 0; break;
                case cc.macro.KEY.s: if(this.vertical == -1) this.vertical = 0; break;
                case cc.macro.KEY.a: if(this.horizontal == -1) this.horizontal = 0; break;
                case cc.macro.KEY.d: if(this.horizontal == 1) this.horizontal = 0; break;
            }
        })
    }
}
